<meta charset='utf-8' />

<body style='padding:10px;color:#838'>
	
	<h2 style='text-align:center' class='title'>医案查询</h2>

	<span style='margin-right:30px'>
		<input type="text" id='s_name' style='width:100px;margin-right:10px' placeholder='医案名关键词'>
		<input type="text" id='s_info' style='width:100px;margin-right:10px' placeholder='内容关键词'>
		<img src="./public/search.png" id='searchBtn' style='cursor:pointer'>
	</span>

	<span style='margin-left:50px;cursor:pointer' id='addBtn' class='userConArea'>添加</span>


	<!--中药列表-->
	<div id='list' style='width:100%'></div>

	<div id='fpage' style='margin:30px 0'></div>


	<!--添加或编辑元素-->
	<div style='position:fixed;top:200px;left:0;width:100%;display:none' id='add_area'>
		<div style='width:450px;margin:0 auto;background:#ccc;padding:20px 0'>
			<h2 style='text-align:center' id='add_title'>编辑中药</h2>
			<div style='display:flex;margin-bottom:20px'>
				<div style='width:100px;text-align:right'>药名：</div>
				<div style='flex:1'><input id='add_name' type="text" style='width:300px'></div>
			</div>
			<div style='display:flex;margin-bottom:20px'>
				<div style='width:100px;text-align:right'>内容：</div>
				<div style='flex:1'><textarea id="add_info" cols="33" rows="5"></textarea></div>
			</div>
			<div style='text-align:center'>
				<button id='add_sub'>提交</button>
				<button id='add_back'>返回</button>
			</div>
		</div>
	</div>

</body>

<script>
var pNow = 1;	//初始当前页
var pMax = 2;	//初始最大页

_init();

function _deleteBing(i){
	if(!_confirm()){
		return;
	}
	var name = list[i].name.trim();
	var data = {
		type:'deleteFang',
		name:name
	}
	Notice('请求提交中......');
	After(data,function(res){
		C(res);
		Notice('');
		var tmp = _checkJsonFormat(res);
		if(tmp.stat == 'ok'){
			A('操作成功！');
			_init();
			$('#add_area').hide();
		}else{
			A('操作异常！');
		}
	});
}

//添加病例
$('#addBtn').click(function(){
	$('#add_area').show();
	$('#add_name').val('');
	$('#add_info').val('');
});

$('#add_back').click(function(){
	$('#add_area').hide();
})


//提交编辑
$('#add_sub').click(function(){
	var name = $('#add_name').val().trim();
	var info = $('#add_info').val().trim();
	if(!name || !info){
		A('至少需要药名和功能');
		return;
	}
	var data = {
		type:'addBing',
		name:name,
		info:info
	}
	Notice('数据提交中......');
	After(data,function(res){
		Notice('');
		var tmp = _checkJsonFormat(res);
		if(tmp.stat == 'ok'){
			A('操作成功！');
			_init();
			$('#add_area').hide();
		}else if(tmp.stat == 'err2'){
			A('医案已存在！');
		}else{
			A('操作异常！');
		}
	});
})








$('#searchBtn').click(function(){
	pNow = 1;
	pMax = 2;
	_init();
});



function _init(){
	Notice('加载中......');
	_getFangList(function(res){
		Notice();
		var tmp = _parseRes(res);
		list = tmp;
		if(tmp.length < 1){
			pMax = pNow;
			Fpage(pMax,pMax,function(p){
				pNow = p;
				pMax = p+1;
				_init();
			},1,1,'#fpage')
			$('#list').html('');
			return;
		}else{
			Fpage(pNow,pMax,function(p){
				pNow = p;
				pMax = p+1;
				_init();
			},1,1,'#fpage')
		}

		var str = '';
		for(var i=0;i<tmp.length;i++){
			str += "<div style='word-break:break-all;margin-top:20px;border-bottom:1px solid #ccc;padding-bottom:10px;font-size:13px'>";
				var c = " style='color:red'";

				var bing_name = $('#s_name').val().trim().split(' ');	//如果有关键字
				str += "<h3 style='color:red'>标题："+_keyBold(tmp[i].name,bing_name)+"<span style='cursor:pointer;color:blue;font-size:13px;margin-left:50px' onclick=_deleteBing("+i+") class='userConArea'>删除</span></h3>";

				str += "<div style='display:flex'>";
					str += "<div>内容：</div>";
					var bing_info = $('#s_info').val().trim().split(' ');
					str += "<div style='flex:1'>"+_keyBold(tmp[i].info,bing_info)+"</div>";
				str += "</div>";

			str += "</div>";
		}
		$('#list').html(str);
		// if(tmp.length < 1){
		// 	pMax = pNow;
		// }
		// Fpage(pNow,pMax,function(p){
		// 	pNow = p;
		// 	pMax = p + 1;
		// 	_init();
		// },1,1,'#fpage')
	});

	_checkUser();	//显示登陆用户及退出
}

//获取中药列表
function _getFangList(fun){
	var data = {
		type:'_getBingList',
		name:$('#s_name').val().trim(),
		info:$('#s_info').val().trim(),
		pNow:pNow
	}
	data.cacheName = md5(JSON.stringify(data));
	var path = './cache/'+data.cacheName;
	//请求缓存
	// Ajax({},function(res){		
	// 	if(/^\[.*\]$/.test(res)){
	// 		C('yao from cache');
	// 		fun(res)
	// 	}else{
	// 		//请求后台
	// 	}
	// },path);
			C('yao from after');
			Ajax2(data,function(res){
				fun(res);
			});

}
</script>


